package ag.script.project.standard.core;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;

import java.io.File;
import java.nio.charset.StandardCharsets;

public class ScriptProjectPackager {

    private final String projectPath;
    private final ScriptProjectStandardLog log;

    public ScriptProjectPackager(String projectPath) {
        this.projectPath = projectPath;
        this.log = new ScriptProjectStandardLog(projectPath);
    }

    public File start() {
        log.log("【压缩资源】");
        if (!FileUtil.exist(projectPath)) {
            log.log("项目路径不存在");
            throw new RuntimeException(" projectPath is not exits !");
        }
        if (!FileUtil.isDirectory(projectPath)) {
            log.log("项目路径不是目录");
            throw new RuntimeException(" projectPath is must a directory !");
        }
        ScriptProject scriptProject = new ScriptProject(projectPath, "", "");
        File zipFile = new File(projectPath, FileUtil.getName(projectPath) + ".zip");
        File fileRes = scriptProject.getFile_res();
        File filePkgJson = scriptProject.getFile_pkg_json();
        log.log("压缩到===>" + zipFile.getAbsolutePath());
        return ZipUtil.zip(zipFile, StandardCharsets.UTF_8, true, fileRes, filePkgJson);
    }
}
